package com.agileboot.admin.controller.airport;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.airport.report.ReportApplicationService;
import com.agileboot.domain.airport.report.command.AddReportCommand;
import com.agileboot.domain.airport.report.command.UpdateReportCommand;
import com.agileboot.domain.airport.report.dto.ReportDTO;
import com.agileboot.domain.airport.report.query.ReportQuery;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.web.SystemLoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 安全信息操作处理 Controller
 */
@Tag(name = "安全信息API", description = "安全信息相关的增删查改")
@RestController
@RequestMapping("/safety/report")
@Validated
@RequiredArgsConstructor
public class SirReportController extends BaseController {

    private final ReportApplicationService reportApplicationService;

    @Operation(summary = "安全信息列表")
//    @PreAuthorize("@permission.has('safety:report:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<ReportDTO>> list(ReportQuery query) {
        PageDTO<ReportDTO> pageDTO = reportApplicationService.getReportList(query);
        return ResponseDTO.ok(pageDTO);
    }

    @Operation(summary = "导出安全信息")
    @AccessLog(title = "安全信息管理", businessType = BusinessTypeEnum.EXPORT)
//    @PreAuthorize("@permission.has('safety:report:export')")
    @GetMapping("/excel")
    public void export(HttpServletResponse response, ReportQuery query) {
        List<ReportDTO> all = reportApplicationService.getReportListAll(query);
        CustomExcelUtil.writeToResponse(all, ReportDTO.class, response);
    }

    @Operation(summary = "安全信息详情")
//    @PreAuthorize("@permission.has('safety:report:query')")
    @GetMapping(value = "/{reportId}")
    public ResponseDTO<ReportDTO> getInfo(@PathVariable Long reportId) {
        ReportDTO report = reportApplicationService.getReportInfo(reportId);
        return ResponseDTO.ok(report);
    }

//    手动上报
    @Operation(summary = "新增安全信息")
//    @PreAuthorize("@permission.has('safety:report:add')")
    @AccessLog(title = "安全信息管理", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody AddReportCommand addCommand) {
        reportApplicationService.addReport(addCommand);
        return ResponseDTO.ok();
    }


    @Operation(summary = "提交问题")
    @AccessLog(title = "提交问题", businessType = BusinessTypeEnum.MODIFY)
    @PostMapping("/submitQuestion")
    public ResponseDTO<Void> submitQuestion(@RequestBody @Valid UpdateReportCommand updateCommand) {
        reportApplicationService.submitReport(updateCommand);
        return ResponseDTO.ok();
    }

    @Operation(summary = "修改安全信息")
//    @PreAuthorize("@permission.has('safety:report:edit')")
    @AccessLog(title = "安全信息管理", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseDTO<Void> edit(@RequestBody UpdateReportCommand updateCommand) {
        // 获取当前用户信息
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        updateCommand.setUpdaterId(loginUser.getUserId());
        updateCommand.setUpdaterBy(loginUser.getUsername());
        reportApplicationService.updateReport(updateCommand);
        return ResponseDTO.ok();
    }

    @Operation(summary = "删除安全信息")
//    @PreAuthorize("@permission.has('safety:report:remove')")
    @AccessLog(title = "安全信息管理", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        reportApplicationService.deleteReport(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }

}
